package com.hotel.customersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.customersystem.mapper.CustomerCollectionMapper;
import com.hotel.customersystem.pojo.CustomerCollection;
import com.hotel.customersystem.service.CustomerCollectionService;
import com.hotel.hotelsystem.mapper.HotelListMapper;
import com.hotel.hotelsystem.pojo.HotelList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * (CustomerCollection)表服务实现类
 *
 * @author makejava
 * @since 2022-09-30 16:06:40
 */
@Service("customerCollectionService")
public class CustomerCollectionServiceImpl extends ServiceImpl<CustomerCollectionMapper, CustomerCollection> implements CustomerCollectionService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CustomerCollectionMapper customerCollectionMapper;

    @Autowired
    private HotelListMapper hotelListMapper;

    /**
     * redis查询用户是否已经收藏
     * 1. 查询 mysql 条件: 1) 用户id 2)酒店id 3)收藏记录状态
     * 2. 将查询到的数据 加载到 redis 当中
     * 3. 查询 redis 当中用户是否收藏
     * @param userId
     */
    @Override
    public Boolean redisSelectIsCollection(String userId,String hotelId){
        //1. 查询 mysql 条件: 1) 用户id 2)酒店id 3)收藏记录状态
        QueryWrapper<CustomerCollection> qw = new QueryWrapper<>();
        qw.select("collection_id");
        qw.eq("hotel_id",hotelId);
        qw.eq("user_id",userId);
        qw.eq("collection_status","1");
        List<CustomerCollection> customerCollections = customerCollectionMapper.selectList(qw);
        //2. 将查询到的数据 加载到 redis 当中
        String hotelIdStr = "hotel:"+hotelId;
        for (CustomerCollection collection : customerCollections) {
            redisTemplate.opsForSet().add(hotelIdStr,collection.getCollectionId());
        }
        //3. 查询 redis 当中用户是否收藏
        Boolean isClick = redisTemplate.opsForSet().isMember(hotelIdStr,userId);
        return isClick;
    }

    /**
     * 添加收藏
     * 1.添加到redis
     * 2.持久化到mysql(需要判断是否存在)
     * @param userId
     * @param hotelId
     * @return
     */
    @Override
    public Boolean redisInsertCollection(String userId, String hotelId) {
        String hotelIdStr = "hotel:"+hotelId;
        //1.添加到redis
        Long result = redisTemplate.opsForSet().add(hotelIdStr, userId);
        if (result != null && !result.equals("")){
            CustomerCollection collection = new CustomerCollection();
            collection.setHotelId(Integer.valueOf(hotelId));
            collection.setUserId(Integer.valueOf(userId));
            collection.setCollectionStatus(1);
            collection.setCollectDatetime(LocalDateTime.now());
            System.out.println("创建对象:"+collection);
            //2.持久化到mysql(需要判断是否存在)
            QueryWrapper<CustomerCollection> qw = new QueryWrapper<>();
            qw.eq("hotel_id",hotelId);
            qw.eq("user_id",userId);
            CustomerCollection collectionBoolean = customerCollectionMapper.selectOne(qw);
            if (collectionBoolean == null) {
                customerCollectionMapper.insert(collection);
                return true;
            }else {
                //存在则进行修改
                collectionBoolean.setCancelDatetime(LocalDateTime.now());
                collectionBoolean.setCollectionStatus(1);
                customerCollectionMapper.updateById(collectionBoolean);
                return true;
            }
        }else {
            return false;
        }
    }

    /**
     * 取消收藏
     * 1.remove redis当中的取消点赞的用户
     * 2.修改mysql当中点赞用户的 1)找到需要修改的收藏 ①用户id ②酒店id 2)修改收藏记录状态为0 3)添加取消时间
     * @param userId
     * @param hotelId
     * @return
     */
    @Override
    public Boolean redisRemoveCollection(String userId, String hotelId) {
        System.out.println("正在删除...");
        String hotelIdStr = "hotel:"+hotelId;
        //1.remove redis当中的取消点赞的用户
        Long result = redisTemplate.opsForSet().remove(hotelIdStr, userId);
        if (result != null && !result.equals("")){
            //2.修改mysql当中点赞用户的
            //1)找到需要修改的收藏 ①用户id ②酒店id
            QueryWrapper<CustomerCollection> qw = new QueryWrapper<>();
            qw.eq("hotel_id",hotelId);
            qw.eq("user_id",userId);
            CustomerCollection collection = customerCollectionMapper.selectOne(qw);
            System.out.println(collection);
            //2)修改收藏记录状态为0
            collection.setCollectionStatus(0);
            //3)添加取消时间
            collection.setCancelDatetime(LocalDateTime.now());
            customerCollectionMapper.updateById(collection);
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取收藏列表
     * @param userId
     * @return
     */
    @Override
    public List<CustomerCollection> getCollectList(Integer userId) {
        QueryWrapper<CustomerCollection> qw = new QueryWrapper<>();
        qw.eq("user_id",userId).eq("collection_status",1).orderByDesc("create_time");
        List<CustomerCollection> customerCollections = customerCollectionMapper.selectList(qw);
        for (CustomerCollection customerCollection : customerCollections) {
            //根据酒店id查找酒店图片名字
            QueryWrapper<HotelList> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("hotel_id",customerCollection.getHotelId());
            HotelList hotelList = hotelListMapper.selectOne(queryWrapper);
            customerCollection.setHotelName(hotelList.getHotelName());
            customerCollection.setHotelImg(hotelList.getHotelShowImg());
            customerCollection.setStarRanting(hotelList.getStarRanting());
            System.out.println("收藏:" + customerCollection);
        }
        return customerCollections;
    }
    @Override
    public Boolean deleteCollection(Integer collectionId) {
        CustomerCollection customerCollection = customerCollectionMapper.selectById(collectionId);
        customerCollection.setCollectionStatus(0);
        customerCollection.setUpdateTime(LocalDateTime.now());
        customerCollection.setCancelDatetime(LocalDateTime.now());
        int row = customerCollectionMapper.updateById(customerCollection);
        if (row>0)
            return true;
        else
            return false;
    }

    @Override
    public Boolean deleteListCollection(List<CustomerCollection> collectionList) {
        int row = 0;
        for (CustomerCollection collection : collectionList) {
            collection.setCollectionStatus(0);
            collection.setUpdateTime(LocalDateTime.now());
            collection.setCancelDatetime(LocalDateTime.now());
            row = row + customerCollectionMapper.updateById(collection);
        }
        if (row>0)
            return true;
        else
            return false;
    }
}
